package com.sunmi.usercenter.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符验证工具
 */
public class CharUtils {

    /**
     * 时间格式，去-
     *
     * @param date
     * @return
     */
    public static String dateChange(String date) {
        StringBuilder sb = new StringBuilder();
        String[] dateStr = date.split("-");
        for (int i = 0; i < dateStr.length; i++) {
            sb.append(dateStr[i]);
        }
        return sb.toString();
    }

    /**
     * 邮箱验证
     *
     * @param str
     * @return
     */
    public static Boolean EmailCheck(String str) {

        if (str.toString().length() > 0 && !str.matches("^\\w+@\\w+\\.(com|cn)")) {
            return false;

        } else
            return true;
    }

    /**
     * 日期验证
     *
     * @param strDate
     * @return
     */
    public static boolean isDate(String strDate) {
        Pattern pattern = Pattern.compile(
                "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
        Matcher m = pattern.matcher(strDate);
        if (m.matches()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 手机号码验证
     *
     * @param phonenum
     * @return
     */
    public static boolean isPhoneNum(String phonenum) {
        Pattern pattern = Pattern.compile("^(1[0-9])\\d{9}$");
        Matcher m = pattern.matcher(phonenum);
        if (m.matches()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 身份证号码验证
     *
     * @param strID
     * @return
     */
    public static boolean isIDNumber(String strID) {
        Pattern pattern = Pattern.compile(
                "((11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65|71|81|82|91)\\d{4})((((19|20)(([02468][048])|([13579][26]))0229))|((20[0-9][0-9])|(19[0-9][0-9]))((((0[1-9])|(1[0-2]))((0[1-9])|(1\\d)|(2[0-8])))|((((0[1,3-9])|(1[0-2]))(29|30))|(((0[13578])|(1[02]))31))))((\\d{3}(x|X))|(\\d{4}))");
        Matcher m = pattern.matcher(strID);
        if (m.matches()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 详细地址验证
     *
     * @param address
     * @return
     */
    public static boolean isAddress(String address) {
        int i = 0, j = 0, k = 0, u = 0;
        int count = address.length();
        Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]");
        Matcher m = pattern.matcher(address);
        while (m.find()) {
            i++;
        }
        for (int idx = 0; idx < count; idx++) {
            char c = address.charAt(idx);
            int tmp = (int) c;
            if ((tmp >= 'a' && tmp <= 'z') || (tmp >= 'A' && tmp <= 'Z')) {
                j++;
            }
            if (Character.isDigit(address.charAt(idx))) {
                k++;
            }
            if (c == ' ') {
                u++;
            }
        }
        if ((i + j + k + u) == count) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串，返回true
     *
     * @param input
     * @return boolean
     */
    public static boolean isEmpty(String input) {
        if (input == null || "".equals(input))
            return true;

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断应用是否安装
     * @param context
     * @param packagename  应用包名
     * @return
     */
    public static boolean isAppInstalled(Context context, String packagename) {
        PackageInfo packageInfo;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(packagename, 0);
        } catch (PackageManager.NameNotFoundException e) {
            packageInfo = null;
            e.printStackTrace();
        }
        if (packageInfo == null) {
            //System.out.println("没有安装");
            return false;
        } else {
            //System.out.println("已经安装");
            return true;
        }
    }

}
